<?php
	
	if (!defined('EXPONENT')) exit('');
	 if (exponent_users_isAdmin() ||  exponent_permissions_check(array("administrate"),$loc)){
		$sql_where = 'location_data = \''.serialize($loc).'\' ';
        if(isset($_REQUEST['search_name']))
        {   
            $search_name = ($_REQUEST['search_name']);
            $sql_where.=" AND (name LIKE '%{$search_name}%')";
        }
		$page=1;
            if (isset($_REQUEST['page']))
			$page=intval($_REQUEST['page']);
		$pageSize=10;
		
		if (isset($_REQUEST['rows']))
			$pageSize=intval($_REQUEST['rows']);
		$start=($page-1)*$pageSize;
		
        // sort
        $sort="";
        if (isset($_REQUEST['sortName'])){
            $sort = " ORDER BY {$_REQUEST['sortName']} {$_REQUEST['sortOrder']}";
        }
        
		$data=$db->selectObjects('linklist_link',"{$sql_where} {$sort} LIMIT {$start},{$pageSize}");
        
        foreach ($data as $k=>$v){
            // split description
            $v->description = substr(strip_tags($v->description),0,200)."...";
            //$v->name = substr(strip_tags($v->name),0,100)."...";
            $v->post_date = strftime(DISPLAY_DATETIME_FORMAT, $v->post_date);
        }
        
		$ret_object = null;
		$ret_object->total = $db->countObjects('linklist_link',"{$sql_where}");
		$ret_object->rows = $data; 
		echo json_encode($ret_object);
	}
	die();
?>